﻿package net.wangit.framework.action;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionContextHelper;
import net.wangit.context.ActionResult;
import net.wangit.framework.config.DomainsSettings;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.DBFactory;
import net.wangit.util.Checker;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ActionUtil {

	private static final Log log = LogFactory
			.getLog(net.wangit.framework.action.ActionUtil.class);

	public ActionUtil() {
	}

	public static DB getActionDB(ActionContext ac) {
		String datasource;
		datasource = ActionContextHelper.getActionDatasource(ac);
		try {
			if (Checker.isEmpty(datasource))
				datasource = DomainsSettings.getDatasource(ActionContextHelper
						.getActionDomain(ac));
			return DBFactory.getDB(datasource);
		} catch (Exception e) {
			ActionResult result = ac.getActionResult();
			result.addError(LabelUtil.get(ac, "system",
					"action.invalid_datasource", datasource));
			log.error((new StringBuilder("create db failed by datasource: "))
					.append(datasource).append(", please check ").append(
							ActionContextHelper.getActionUnitId(ac))
					.append("/").append(ActionContextHelper.getActionId(ac))
					.toString(), e);
			return null;
		}
	}
}
